import { checkToken, setLogout, getMenu, getPermission } from "../utils/request"
import { isNull } from "../utils/helper"
import { Modal } from 'antd-mobile';
export default {
	namespace: 'global',
	state: {
		count: 0,
		globalMenu: [],
		globalLeftMenu: [],
		headerTitle: {},
		modalPath: [],
		navigate: {},
		changeRefreshing:{},
		listviewRowApiKeys: {}
	},


	subscriptions: {},
	effects: {
		*loadMenu({ payload }, { put, call, select }) {
			let { callback } = payload;
			let resultToken = yield call(checkToken);
			console.log("resultToken", resultToken)
			if (resultToken.code != 10200) {
				setLogout({ ...resultToken, code1: 22 })
			}
			else {
				if (isNull(localStorage[MI_APP_CODE+"user_id"]) || isNull(localStorage[MI_APP_CODE+"menus"])) {
					setLogout({ ...resultToken, code1: 26 })
					return;
				}
				let default_page = localStorage[MI_APP_CODE+"default_page"];
				let default_page_title = "";
				let menus = JSON.parse(localStorage[MI_APP_CODE+"menus"]);
				//menus是个数组[{code:"tabs"},{code:"leftmenu"}] tabs是底部导航 leftmenu为左侧菜单导航
				let globalMenu = [];
				let globalLeftMenu = [];
				let menu = menus.filter(item => item.code == "tabs")[0].children;
				menu = menu.filter(item => {
					let { is_app } = item;
					if (is_app == 1) {//属于app
						return false;
					}
					return true;
				})
				let leftMenu = menus.filter(item => item.code == "leftmenu");
				if (leftMenu.length > 0) {
					leftMenu = leftMenu[0].children;
				}
				else {
					leftMenu = []
				}
				leftMenu = leftMenu.filter(item => {
					if (item.is_app == 1) {//属于app
						return false
					}
					return true;
				})
				leftMenu.map(item => {
					globalLeftMenu.push({
						...JSON.parse(item.config_json),
						code: item.code
					})
				});
				menu.map(item => {
					globalMenu.push({
						...JSON.parse(item.config_json),
						code: item.code
					}
					)
				});
				// default_page="/page?_c=test_form";
				yield put({
					type: "setState",
					payload: {
						globalMenu,
						globalLeftMenu,
						default_page,
						default_page_title
					}
				});
				callback && callback({ globalMenu, globalLeftMenu, default_page, default_page_title })
			}
		},
		*setHeaderTitle({ payload }, { put, call, select }) {
			let { title, path } = payload;
			let headerTitle = yield select(state => state.global.headerTitle);
			headerTitle = {
				...headerTitle,
				[path]: title
			};
			yield put({
				type: "setState",
				payload: {
					headerTitle
				}
			})
		},
		*setNavigate({ payload }, { put, call, select }) {
			let oldNavigate = yield select(state => state.global.navigate);
			let { path, navigate, config } = payload;
			yield put({
				type: "setState",
				payload: {
					navigate: {
						...oldNavigate,
						[path]: {
							navigate,
							config
						}
					}
				}
			})
		},
		*addModal({ payload }, { put, call, select }) {
			let { config } = payload;
			let modalPath = yield select(state => state.global.modalPath);
			let newModalPath = [];
			if (!modalPath || modalPath.length == 0) {
				newModalPath = [config]
			}
			else {
				modalPath.map(item => {
					newModalPath.push(item)
				});
				newModalPath.push(config)
			}
			yield put({
				type: "setState",
				payload: {
					modalPath: newModalPath
				}
			});
			localStorage[MI_APP_CODE+"modalPath"] = JSON.stringify(newModalPath)
		},
		*closeModal({ payload }, { put, call, select }) {
			let { _key } = payload;
			let modalPath = yield select(state => state.global.modalPath);
			let newModalPath = modalPath.filter(item => item._key !== _key);
			yield put({
				type: "setState",
				payload: {
					modalPath: newModalPath
				}
			});
			localStorage[MI_APP_CODE+"modalPath"] = JSON.stringify(newModalPath);
			if (newModalPath.length == 0) {
				document.getElementById("root").style.overflow = "unset"
			}
		},
		*clearModal({ payload }, { put, call, select }) {
			yield put({
				type: "setState",
				payload: {
					modalPath: []
				}
			});
			localStorage[MI_APP_CODE+"modalPath"] = "[]";
			document.getElementById("root").style.overflow = "unset"
		},
		*isChangeRefreshing({payload},{put,all,select}){
			let changeRefreshing = yield select(state => state.global.changeRefreshing);
			console.log(changeRefreshing);
			let {key}=payload;
			changeRefreshing={
				...changeRefreshing,
				[key]:!changeRefreshing[key]
			}
			yield put({
				type:"setState",
				payload:{
					changeRefreshing
				}
			})
		},
		*setListviewRowApiKey({ payload }, { put, call, select }) {
			let listviewRowApiKeys = yield select(state => state.global.listviewRowApiKeys);
			let { key, value } = payload;
			listviewRowApiKeys = {
				...listviewRowApiKeys,
				[key]: value
			}
			yield put({
				type: "setState",
				payload: {
					listviewRowApiKeys
				}
			})
		},
		*removeListviewRowApiKey({ payload }, { put, call, select }) {
			let { key } = payload;
			let listviewRowApiKeys = yield select(state => state.global.listviewRowApiKeys);
			delete listviewRowApiKeys[key];
			yield put({
				type: "setState",
				payload: {
					listviewRowApiKeys
				}
			})
		}
	},
	reducers: {
		setState(state, action) {
			return {
				...state, ...action.payload
			}
		}
	}
};
